
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>逐行解读trigger和调度器 · petite-vue源码剖析</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="^_^肥仔John">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-back-to-top-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-donate/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-theme-comscore/test.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../3_sandbox.html" />
    
    
    <link rel="prev" href="5_smi.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">前言</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    《Petite-Vue源码剖析》简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../0_intro/1_reason_for_source_reading.html">
            
                <a href="../0_intro/1_reason_for_source_reading.html">
            
                    
                    为什么要读源码
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">在线渲染</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../1_online_rendering/0_intro.html">
            
                <a href="../1_online_rendering/0_intro.html">
            
                    
                    章节介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../1_online_rendering/1_start_from_static_view.html">
            
                <a href="../1_online_rendering/1_start_from_static_view.html">
            
                    
                    从静态视图首次渲染开始
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../1_online_rendering/2_initial_rendering_and_rerendering_for_v-if.html">
            
                <a href="../1_online_rendering/2_initial_rendering_and_rerendering_for_v-if.html">
            
                    
                    条件v-if的首次渲染和重新渲染
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../1_online_rendering/3_initial_rendering_and_rerendering_for_v-for.html">
            
                <a href="../1_online_rendering/3_initial_rendering_and_rerendering_for_v-for.html">
            
                    
                    循环v-for的首次渲染和重新渲染
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="../1_online_rendering/4_directives/1_attribute_binding.html">
            
                <a href="../1_online_rendering/4_directives/1_attribute_binding.html">
            
                    
                    属性绑定v-bind的工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="../1_online_rendering/4_directives/2_event_binding.html">
            
                <a href="../1_online_rendering/4_directives/2_event_binding.html">
            
                    
                    事件绑定v-on的工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="../1_online_rendering/4_directives/3_bidirectional_binding.html">
            
                <a href="../1_online_rendering/4_directives/3_bidirectional_binding.html">
            
                    
                    双向绑定v-model的工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="../1_online_rendering/4_directives/4_ref.html">
            
                <a href="../1_online_rendering/4_directives/4_ref.html">
            
                    
                    特殊指令ref的工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="../1_online_rendering/5_tips_for_template.html">
            
                <a href="../1_online_rendering/5_tips_for_template.html">
            
                    
                    优化手段template的原理和使用技巧
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">响应式系统</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="0_intro.html">
            
                <a href="0_intro.html">
            
                    
                    章节介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="1_reactive_object_array.html">
            
                <a href="1_reactive_object_array.html">
            
                    
                    逐行解读reactive如何构建对象和数组的响应式对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="2_reactive_map_set.html">
            
                <a href="2_reactive_map_set.html">
            
                    
                    逐行解读reactive如何构建Map和Set的响应式对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="3_register_side_effect.html">
            
                <a href="3_register_side_effect.html">
            
                    
                    逐行解读effect如何注册副作用函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="4_cleanup_algorithm.html">
            
                <a href="4_cleanup_algorithm.html">
            
                    
                    依赖清理优化算法详解-算法思路
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="5_smi.html">
            
                <a href="5_smi.html">
            
                    
                    依赖清理优化算法详解-基于JavaScript引擎的SMI进一步压榨性能
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="3.7" data-path="6_trigger_and_scheduler.html">
            
                <a href="6_trigger_and_scheduler.html">
            
                    
                    逐行解读trigger和调度器
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">沙箱模型</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../3_sandbox.html">
            
                <a href="../3_sandbox.html">
            
                    
                    模板表达式的沙箱模型
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >逐行解读trigger和调度器</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><li><span class="title-icon "></span><a href="#&#x9010;&#x884C;&#x89E3;&#x8BFB;trigger&#x548C;&#x8C03;&#x5EA6;&#x5668;"><b>1. </b>&#x9010;&#x884C;&#x89E3;&#x8BFB;trigger&#x548C;&#x8C03;&#x5EA6;&#x5668;</a></li><ul><li><span class="title-icon "></span><a href="#&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x89E6;&#x53D1;&#x5668;trigger"><b>1.1. </b>&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x89E6;&#x53D1;&#x5668;Trigger</a></li><li><span class="title-icon "></span><a href="#&#x8C03;&#x5EA6;&#x5668;scheduler"><b>1.2. </b>&#x8C03;&#x5EA6;&#x5668;Scheduler</a></li></ul></ul></div><h1 id="&#x9010;&#x884C;&#x89E3;&#x8BFB;trigger&#x548C;&#x8C03;&#x5EA6;&#x5668;"><a name="&#x9010;&#x884C;&#x89E3;&#x8BFB;trigger&#x548C;&#x8C03;&#x5EA6;&#x5668;" class="anchor-navigation-ex-anchor" href="#&#x9010;&#x884C;&#x89E3;&#x8BFB;trigger&#x548C;&#x8C03;&#x5EA6;&#x5668;"><i class="fa fa-link" aria-hidden="true"></i></a>1. &#x9010;&#x884C;&#x89E3;&#x8BFB;trigger&#x548C;&#x8C03;&#x5EA6;&#x5668;</h1>
<h2 id="&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x89E6;&#x53D1;&#x5668;trigger"><a name="&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x89E6;&#x53D1;&#x5668;trigger" class="anchor-navigation-ex-anchor" href="#&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x89E6;&#x53D1;&#x5668;trigger"><i class="fa fa-link" aria-hidden="true"></i></a>1.1. &#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x89E6;&#x53D1;&#x5668;Trigger</h2>
<p>&#x7531;&#x4E8E;&#x5728;&#x8BB2;&#x89E3;&quot;&#x4F18;&#x5316;&#x65E0;&#x7528;&#x4F9D;&#x8D56;&#x6E05;&#x7406;&#x7B97;&#x6CD5;&quot;&#x65F6;&#x5DF2;&#x7ECF;&#x5BF9;<code>track</code>&#x8FDB;&#x884C;&#x4E86;&#x5256;&#x6790;&#xFF0C;&#x56E0;&#x6B64;&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x76F4;&#x63A5;&#x5206;&#x6790;<code>trigger</code>&#x5C31;&#x597D;&#x4E86;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">trigger</span>(<span class="hljs-params">
  target: object,
  <span class="hljs-comment">// set, add, delete, clear</span>
  <span class="hljs-keyword">type</span>: TriggerOpTypes,
  key?: unknown,
  newValue?: unknown,
  oldValue?: unknown,
  oldTarget?: Map&lt;unknown, unknown&gt; | Set&lt;unknown&gt;
</span>) </span>{
  <span class="hljs-keyword">const</span> depsMap = targetMap.get(target)
  <span class="hljs-keyword">if</span> (!depsMap) {
    <span class="hljs-comment">// &#x8BE5;&#x5C5E;&#x6027;&#x6CA1;&#x6709;&#x88AB;&#x4EFB;&#x4F55;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x8DDF;&#x8E2A;&#x8FC7;&#xFF0C;&#x6240;&#x4EE5;&#x76F4;&#x63A5;&#x8FD4;&#x56DE;&#x5C31;&#x597D;&#x4E86;</span>
    <span class="hljs-keyword">return</span>
  }

  <span class="hljs-comment">/**
   * &#x7528;&#x4E8E;&#x5B58;&#x50A8;&#x5C06;&#x8981;&#x88AB;&#x89E6;&#x53D1;&#x7684;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x3002;
   * &#x4E3A;&#x4EC0;&#x4E48;&#x4E0D;&#x76F4;&#x63A5;&#x901A;&#x8FC7;&#x7C7B;&#x4F3C;depsMap.values().forEach(fn =&gt; fn())&#x6267;&#x884C;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x5462;&#xFF1F;
   * &#x90A3;&#x662F;&#x56E0;&#x4E3A;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x6267;&#x884C;&#x65F6;&#x53EF;&#x80FD;&#x4F1A;&#x5220;&#x9664;&#x6216;&#x589E;&#x52A0;depsMap.values()&#x7684;&#x5143;&#x7D20;&#xFF0C;&#x5BFC;&#x81F4;&#x5176;&#x4E2D;&#x7684;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x6267;&#x884C;&#x5F02;&#x5E38;&#x3002;
   * &#x56E0;&#x6B64;&#x7528;&#x53E6;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x5B58;&#x50A8;&#x5C06;&#x8981;&#x6267;&#x884C;&#x7684;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x96C6;&#x5408;&#xFF0C;&#x90A3;&#x4E48;&#x6267;&#x884C;&#x8FC7;&#x7A0B;&#x4E2D;&#x4FEE;&#x6539;&#x7684;&#x662F;depsMap.values()&#x7684;&#x5143;&#x7D20;&#xFF0C;&#x800C;&#x6B63;&#x5728;&#x904D;&#x5386;&#x6267;&#x884C;&#x7684;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x96C6;&#x5408;&#x7ED3;&#x6784;&#x662F;&#x7A33;&#x5B9A;&#x7684;&#x3002;
   */</span>
  <span class="hljs-keyword">let</span> deps: (Dep | <span class="hljs-literal">undefined</span>)[] = []
  <span class="hljs-keyword">if</span> (<span class="hljs-keyword">type</span> === TriggerOpTypes.CLEAR) {
    <span class="hljs-comment">// &#x5BF9;&#x8C61;&#x7684;&#x6240;&#x6709;&#x5C5E;&#x6027;&#x503C;&#x6E05;&#x7A7A;&#xFF0C;&#x6240;&#x6709;&#x4F9D;&#x8D56;&#x8BE5;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;&#x7684;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x90FD;&#x5C06;&#x88AB;&#x89E6;&#x53D1;</span>
    deps = [...depsMap.values()]
  }
  <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (key === <span class="hljs-string">&apos;length&apos;</span> &amp;&amp; isArray(target)) {
    <span class="hljs-comment">// &#x82E5;&#x8BBE;&#x7F6E;length&#x5C5E;&#x6027;&#xFF0C;&#x90A3;&#x4E48;&#x4F9D;&#x8D56;length&#x5C5E;&#x6027;&#x548C;&#x7D22;&#x5F15;&#x503C;&#x5927;&#x4E8E;&#x7B49;&#x4E8E;&#x65B0;&#x7684;length&#x5C5E;&#x6027;&#x503C;&#x7684;&#x5143;&#x7D20;&#x7684;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x90FD;&#x4F1A;&#x88AB;&#x89E6;&#x53D1;</span>
    depsMap.forEach((dep, key) =&gt; {
      <span class="hljs-keyword">if</span> (key === <span class="hljs-string">&apos;length&apos;</span> || key &gt;= (newValue as <span class="hljs-built_in">number</span>)) {
        deps.push(dep)
      }
    })
  }
  <span class="hljs-keyword">else</span> {
    <span class="hljs-comment">// &#x5C06;&#x4F9D;&#x8D56;&#x8BE5;&#x5C5E;&#x6027;&#x7684;</span>
    <span class="hljs-keyword">if</span> (key !== <span class="hljs-built_in">void</span> <span class="hljs-number">0</span>) {
      <span class="hljs-comment">// &#x5373;&#x4F7F;&#x63D2;&#x5165;&#x7684;&#x662F;undefined&#x4E5F;&#x6CA1;&#x6709;&#x5173;&#x7CFB;</span>
      deps.push(depsMap.get(key))
    }

    <span class="hljs-comment">/**
     * &#x6DFB;&#x52A0;&#x95F4;&#x63A5;&#x4F9D;&#x8D56;&#x7684;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;
     * 1. &#x65B0;&#x589E;&#x6570;&#x7EC4;&#x65B0;&#x503C;&#x7D22;&#x5F15;&#x5927;&#x4E8E;&#x6570;&#x7EC4;&#x957F;&#x5EA6;&#x65F6;&#xFF0C;&#x4F1A;&#x5BFC;&#x81F4;&#x6570;&#x7EC4;&#x5BB9;&#x91CF;&#x88AB;&#x6269;&#x5145;&#xFF0C;length&#x5C5E;&#x6027;&#x4E5F;&#x4F1A;&#x53D1;&#x751F;&#x53D8;&#x5316;
     * 2. &#x65B0;&#x589E;&#x6216;&#x5220;&#x9664;Set/WeakSet/Map/WeakMap&#x5143;&#x7D20;&#x65F6;&#xFF0C;&#x9700;&#x8981;&#x89E6;&#x53D1;&#x4F9D;&#x8D56;&#x8FED;&#x4EE3;&#x5668;&#x7684;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;
     * 3. &#x65B0;&#x589E;&#x6216;&#x5220;&#x9664;Map/WeakMap&#x5143;&#x7D20;&#x65F6;&#xFF0C;&#x9700;&#x8981;&#x89E6;&#x53D1;&#x4F9D;&#x8D56;&#x952E;&#x8FED;&#x4EE3;&#x5668;&#x7684;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;
     * 4. &#x8BBE;&#x7F6E;Map/WeakMap&#x5143;&#x7D20;&#x7684;&#x503C;&#x65F6;&#xFF0C;&#x9700;&#x8981;&#x89E6;&#x53D1;&#x4F9D;&#x8D56;&#x8FED;&#x4EE3;&#x5668;&#x7684;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;
     */</span> 
    <span class="hljs-keyword">switch</span>(<span class="hljs-keyword">type</span>) {
      <span class="hljs-keyword">case</span> TriggerOpTypes.ADD:
        <span class="hljs-keyword">if</span> (!isArray(target)) {
          <span class="hljs-comment">// &#x5BF9;&#x4E8E;&#x975E;&#x6570;&#x7EC4;&#xFF0C;&#x5219;&#x89E6;&#x53D1;&#x901A;&#x8FC7;&#x8FED;&#x4EE3;&#x5668;&#x904D;&#x5386;&#x7684;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;</span>
          deps.push(depsMap.get(ITERATE_KEY))
          <span class="hljs-keyword">if</span> (isMap(target)) {
            deps.push(depsMap.get(MAP_KEY_ITERATE_KEY))
          }
        }
        <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (isIntegerKey(key)) {
          <span class="hljs-comment">// &#x5BF9;&#x6570;&#x7EC4;&#x63D2;&#x5165;&#x65B0;&#x5143;&#x7D20;&#xFF0C;&#x5219;&#x9700;&#x8981;&#x89E6;&#x53D1;&#x4F9D;&#x8D56;length&#x7684;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;</span>
          deps.push(depsMap.get(<span class="hljs-string">&apos;length&apos;</span>))
        }
        <span class="hljs-keyword">break</span>
      <span class="hljs-keyword">case</span> TriggerOpTypes.DELETE:
        <span class="hljs-keyword">if</span> (!isArray(target)) {
          <span class="hljs-comment">// &#x5BF9;&#x4E8E;&#x975E;&#x6570;&#x7EC4;&#xFF0C;&#x5219;&#x89E6;&#x53D1;&#x901A;&#x8FC7;&#x8FED;&#x4EE3;&#x5668;&#x904D;&#x5386;&#x7684;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;</span>
          deps.push(depsMap.get(ITERATE_KEY))
          <span class="hljs-keyword">if</span> (isMap(target)) {
            deps.push(depsMap.get(MAP_KEY_ITERATE_KEY))
          }
        }
        <span class="hljs-keyword">break</span>
      <span class="hljs-keyword">case</span> TriggerOpTypes.SET:
        <span class="hljs-comment">// &#x5BF9;&#x4E8E;Map/WeakMap&#x9700;&#x8981;&#x89E6;&#x53D1;&#x4F9D;&#x8D56;&#x8FED;&#x4EE3;&#x5668;&#x7684;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;</span>
        <span class="hljs-keyword">if</span> (isMap(target)) {
          deps.push(depsMap.get(ITERATE_KEY))
        }
    }

    <span class="hljs-keyword">if</span> (deps.length === <span class="hljs-number">1</span>) {
      <span class="hljs-comment">// &#x8FC7;&#x6EE4;&#x6389;undefined</span>
      <span class="hljs-keyword">if</span> (deps[<span class="hljs-number">0</span>]) {
        triggerEffects(deps[<span class="hljs-number">0</span>])
      }
    }
    <span class="hljs-keyword">else</span> {
      <span class="hljs-keyword">const</span> effects: ReactiveEffect[] = []
      <span class="hljs-comment">// &#x8FC7;&#x6EE4;&#x6389;undefined</span>
      <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> dep of deps) {
        <span class="hljs-keyword">if</span> (dep) {
          effects.push(...dep)
        }
      }
      triggerEffects(createDep(effects))
    }
  }
}

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">triggerEffects</span>(<span class="hljs-params">
  dep: Dep | ReactiveEffect[]
</span>) </span>{
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> effect of isArray(dep) ? dep : [...dep]) {
    <span class="hljs-comment">/**
     * &#x5FC5;&#x987B;&#x4FDD;&#x8BC1;&#x5C06;&#x8981;&#x89E6;&#x53D1;&#x7684;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;(effect)&#x4E0D;&#x662F;&#x5F53;&#x524D;&#x8FD0;&#x884C;&#x7684;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;(activeEffect)&#xFF0C;&#x5426;&#x5219;&#x5C06;&#x5D4C;&#x5165;&#x65E0;&#x9650;&#x9012;&#x5F52;&#x3002;
     * &#x5047;&#x8BBE;&#x5B58;&#x5728;&#x5982;&#x4E0B;&#x60C5;&#x51B5;
     * let foo = reactive({ bar: 1 })
     * effect(() =&gt; {
     *   foo.bar = foo.bar + 1
     * })
     * &#x82E5;&#x6CA1;&#x6709;&#x4E0A;&#x8FF0;&#x7684;&#x4FDD;&#x969C;&#xFF0C;&#x5219;&#x5C06;&#x4F1A;&#x4E0D;&#x65AD;&#x9012;&#x5F52;&#x4E0B;&#x53BB;&#x76F4;&#x63A5;&#x7206;&#x6808;&#x3002;
     * 
     * &#x5047;&#x5982;ReactiveEffect&#x5BF9;&#x8C61;&#x7684;allowRecurse&#x8BBE;&#x7F6E;&#x4E3A;true&#xFF0C;&#x90A3;&#x4E48;&#x8868;&#x793A;&#x4E0D;&#x5BF9;&#x4E0A;&#x8FF0;&#x95EE;&#x9898;&#x4F5C;&#x9632;&#x5FA1;&#x3002;
     */</span> 
    <span class="hljs-keyword">if</span> (effect !== activeEffect || effect.allowRecurse) {
      <span class="hljs-keyword">if</span> (effect.scheduler) {
        <span class="hljs-comment">// &#x82E5;&#x8BBE;&#x7F6E;&#x6709;&#x8C03;&#x5EA6;&#x5668;&#x5219;&#x8C03;&#x7528;&#x8C03;&#x7528;&#x5668;</span>
        effect.scheduler()
      }
      <span class="hljs-keyword">else</span> {
        <span class="hljs-comment">// &#x7ACB;&#x5373;&#x6267;&#x884C;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;</span>
        effect.run()
      }
    }
  }
}
</code></pre>
<h2 id="&#x8C03;&#x5EA6;&#x5668;scheduler"><a name="&#x8C03;&#x5EA6;&#x5668;scheduler" class="anchor-navigation-ex-anchor" href="#&#x8C03;&#x5EA6;&#x5668;scheduler"><i class="fa fa-link" aria-hidden="true"></i></a>1.2. &#x8C03;&#x5EA6;&#x5668;Scheduler</h2>
<p>&#x5728;&#x4E0A;&#x4E00;&#x8282;&#x7684;<code>triggerEffects</code>&#x4E2D;&#x6211;&#x4EEC;&#x770B;&#x5230;&#x9ED8;&#x8BA4;&#x91C7;&#x7528;&#x540C;&#x6B65;&#x65B9;&#x5F0F;&#x6267;&#x884C;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#xFF0C;&#x82E5;&#x8981;&#x540C;&#x6B65;&#x6267;&#x884C;&#x6570;&#x5341;&#x4E2A;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x90A3;&#x4E48;&#x52BF;&#x5FC5;&#x4F1A;&#x5F71;&#x54CD;&#x5F53;&#x524D;&#x4E8B;&#x4EF6;&#x5FAA;&#x73AF;&#x4E3B;&#x903B;&#x8F91;&#x7684;&#x6267;&#x884C;&#xFF0C;&#x8FD9;&#x65F6;&#x5C31;&#x662F;&#x8C03;&#x5EA6;&#x5668;&#x95EA;&#x4EAE;&#x767B;&#x573A;&#x7684;&#x65F6;&#x5019;&#x4E86;&#x3002;&#x6211;&#x4EEC;&#x56DE;&#x987E;&#x4EE5;&#x4E0B;petite-vue&#x4E2D;&#x63D0;&#x4F9B;&#x7684;&#x8C03;&#x5EA6;&#x5668;&#x5427;!</p>
<pre><code class="lang-ts"><span class="hljs-keyword">import</span> { effect as rawEffect } from <span class="hljs-string">&apos;@vue/reactivity&apos;</span>

<span class="hljs-keyword">const</span> effect = (fn) =&gt; {
  <span class="hljs-keyword">const</span> e: ReactiveEffectRunner = rawEffect(fn, {
    scheduler: () =&gt; queueJob(e)
  })
  <span class="hljs-keyword">return</span> e
}
</code></pre>
<pre><code class="lang-ts"><span class="hljs-comment">// ./scheduler.ts</span>

<span class="hljs-keyword">let</span> queued = <span class="hljs-literal">false</span>
<span class="hljs-keyword">const</span> queue: <span class="hljs-built_in">Function</span>[] = []
<span class="hljs-keyword">const</span> p = Promise.resolve()

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> nextTick = (fn: () =&gt; <span class="hljs-built_in">void</span>) =&gt; p.then(fn)

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> queueJob = (job: <span class="hljs-built_in">Function</span>) =&gt; {
  <span class="hljs-keyword">if</span> (!queue.includes(job)) queue.push(job)
  <span class="hljs-keyword">if</span> (!queued) {
    queued = <span class="hljs-literal">true</span>
    nextTick(flushJobs)
  }
}

<span class="hljs-keyword">const</span> flushJobs = () =&gt; {
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> job of queue) {
    job()
  }
  queue.length = <span class="hljs-number">0</span>
  queued = <span class="hljs-literal">false</span>
}
</code></pre>
<p>&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x538B;&#x5165;&#x961F;&#x5217;&#x4E2D;&#xFF0C;&#x5E76;&#x5C06;&#x904D;&#x5386;&#x961F;&#x5217;&#x6267;&#x884C;&#x5176;&#x4E2D;&#x7684;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x540E;&#x6E05;&#x7A7A;&#x961F;&#x5217;&#x7684;<code>flushJobs</code>&#x538B;&#x5165;micro queue&#x3002;&#x90A3;&#x4E48;&#x5F53;&#x524D;&#x4E8B;&#x4EF6;&#x5FAA;&#x73AF;&#x4E3B;&#x903B;&#x8F91;&#x6267;&#x884C;&#x5B8C;&#x540E;&#xFF0C;JavaScript&#x5F15;&#x64CE;&#x5C06;&#x4F1A;&#x6267;&#x884C;micro queue&#x4E2D;&#x7684;&#x6240;&#x6709;&#x4EFB;&#x52A1;&#x3002;</p>
<script type="text/javascript">
  /**
   * Donation
   */
  function showQR(){
    var rewardButton = document.getElementById('rewardButton')
    var qr = document.getElementById('QR')
    if (rewardButton) {
      rewardButton.style.display = 'none'
    }
    if (qr) {
      qr.style.display = 'block'
    }
  }
  setInterval(showQR, 1000)

  /**
   * Baidu analysis
   */
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?8bca1e3aa362a7f85dbe8304cff65225";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
</script>


<footer class="page-footer"><span class="copyright">Copyright &#xA9; fsjohnhuang 2022 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">File Modify: 
2022-04-26 09:47:39
</span></footer>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="5_smi.html" class="navigation navigation-prev " aria-label="Previous page: 依赖清理优化算法详解-基于JavaScript引擎的SMI进一步压榨性能">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../3_sandbox.html" class="navigation navigation-next " aria-label="Next page: 模板表达式的沙箱模型">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"逐行解读trigger和调度器","level":"3.7","depth":1,"next":{"title":"模板表达式的沙箱模型","level":"4.1","depth":1,"path":"3_sandbox.md","ref":"3_sandbox.md","articles":[]},"previous":{"title":"依赖清理优化算法详解-基于JavaScript引擎的SMI进一步压榨性能","level":"3.6","depth":1,"path":"2_reactivity_system/5_smi.md","ref":"2_reactivity_system/5_smi.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-lunr","-search","-sharing","theme-comscore","search-pro","code","back-to-top-button","tbfed-pagefooter","chapter-fold","expandable-chapters","splitter","anchor-navigation-ex","donate","sharing-plus","baidu"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy fsjohnhuang 2022"},"chapter-fold":{},"splitter":{},"search-pro":{},"sharing-plus":{"qq":false,"all":["facebook","google","twitter","instapaper","linkedin","pocket","stumbleupon"],"douban":false,"facebook":true,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":true,"messenger":false,"line":false,"vk":false,"pocket":true,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"code":{"copyButtons":true},"donate":{"alipay":"","alipayText":"支付宝打赏","button":"赞赏","title":"","wechat":"/petite-vue-source-reading/images/wechat.jpg","wechatText":"微信打赏"},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":false,"showLevel":true},"theme-comscore":{},"back-to-top-button":{},"baidu":{"token":"8bca1e3aa362a7f85dbe8304cff65225"},"sharing":{"qq":false,"all":["qq","qzone","weibo","whatsapp","twitter","facebook","line"],"douban":false,"facebook":false,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":false,"messenger":false,"line":false,"vk":false,"pocket":false,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"expandable-chapters":{}},"theme":"default","author":"^_^肥仔John","lang":"zh-cn","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"isbn":"","variables":{},"title":"petite-vue源码剖析","gitbook":"*","description":""},"file":{"path":"2_reactivity_system/6_trigger_and_scheduler.md","mtime":"2022-04-26T01:47:39.887Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-05-05T01:20:11.998Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-back-to-top-button/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-donate/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing-plus/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-baidu/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-theme-comscore/test.js"></script>
        
    

    </body>
</html>

